/*
 * RedPacketLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 红包布局
 */
public class RedPacketLayout extends LinearLayout {

    public static final int RED_PACKET = 0;

    public static final int COUPON = 1;

    private TextView mTvMoney;
    private TextView mTvName;
    private TextView mTvType;
    private TextView mTvLimit;
    private TextView mTvTime;
    private ImageView mIvBg;
    private ImageView mIvStatus;
    private CheckBox mCheckbox;
    private TextView mTvChoose;
    private OnChooseGoodsClickListener mListener;

    public RedPacketLayout(Context context) {
        this(context, null);
    }

    public RedPacketLayout(Context context, AttributeSet attr) {
        super(context, attr);

        TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.RedPacketLayout);

        int type = typedArray.getInt(R.styleable.RedPacketLayout_layout_type, 0);

        typedArray.recycle();

        View rootView = View.inflate(context, R.layout.red_packet, this);

        mCheckbox = (CheckBox) rootView.findViewById(R.id.checkbox);

        mIvStatus = (ImageView) rootView.findViewById(R.id.iv_status);

        mTvMoney = (TextView) rootView.findViewById(R.id.tv_money);
        mTvName = (TextView) rootView.findViewById(R.id.tv_name);
        mTvType = (TextView) rootView.findViewById(R.id.tv_type);
        mTvLimit = (TextView) rootView.findViewById(R.id.tv_limit);
        mTvTime = (TextView) rootView.findViewById(R.id.tv_time);
        mTvChoose = (TextView) rootView.findViewById(R.id.tv_to_choose_goods);
        mIvBg = (ImageView) rootView.findViewById(R.id.iv_bg);

        setType(type);
        initEvent();
    }

    private void initEvent() {
        mTvChoose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.click();
                }
            }
        });
    }

    public void setRedPacketData(
            String money, String name, String type, String limit, String time) {
        SpannableString priceString =
                TextRenderUtils.getBeautyPrice(money, "", UIUtils.dp2px(36), UIUtils.dp2px(14));
        mTvMoney.setText(priceString);
        mTvName.setText(name);
        mTvType.setText(type);
        mTvLimit.setText(limit);
        mTvTime.setText(time);
    }

    /**
     * 设置是红包 还是优惠券
     */
    public void setType(int type) {
        switch (type) {
            case RED_PACKET:
                mIvBg.setBackgroundResource(R.mipmap.ic_red_packet_bg);
                mTvChoose.setTextColor(UIUtils.getColor(R.color.colorSelector));
                mTvChoose.setBackgroundResource(R.drawable.bg_btn_stroke_red_normal);
                break;
            case COUPON:
                mIvBg.setBackgroundResource(R.mipmap.ic_coupon_bg);
                mTvChoose.setVisibility(GONE);
                mTvChoose.setTextColor(UIUtils.getColor(R.color.coupon_to_choose_goods));
                mTvChoose.setBackgroundResource(R.drawable.bg_btn_stroke_blue_normal);
                break;
        }
    }

    /**
     * 设置红包 优惠券是否可用
     *
     * @param isGone 显示模式
     * @param isUsed 是已使用  还是过期
     */
    public void setTipStatus(boolean isGone, boolean isUsed) {
        if (isGone) {
            mTvMoney.setTextColor(UIUtils.getColor(R.color.colorSelector));
            mIvStatus.setVisibility(GONE);
        } else {
            mTvMoney.setTextColor(UIUtils.getColor(R.color.red_packet_unuse));
            mIvStatus.setVisibility(VISIBLE);
            mIvBg.setBackgroundResource(R.mipmap.ic_unuse_bg);
            mIvStatus.setImageResource(
                    isUsed ? R.mipmap.ic_redpacket_used : R.mipmap.ic_redpacket_unuse);
        }
    }

    /**
     * 选购商品是否可见
     */
    public void setChooseGoodsVisible(boolean visible) {
        mTvChoose.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 是否显示checkbox
     */
    public void setCanChoose(boolean canChoose) {
        mCheckbox.setVisibility(canChoose ? VISIBLE : GONE);
    }

    /**
     * 设置checkbox是否选中
     */
    public void setChecked(boolean checked) {
        mCheckbox.setChecked(checked);
    }

    public interface OnChooseGoodsClickListener {
        void click();
    }

    public void setOnChooseGoodsClickListener(OnChooseGoodsClickListener listener) {
        mListener = listener;
    }
}
